package com.kaifamiao.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.logging.Logger;

// 如果多个采用 @WebFilter 注解标注 Filter 类 对同一个资源的访问进行过滤
// 则按照 Filter 类的类名的 自然顺序 执行 过滤
public class WelcomeFilter implements Filter {

    private Logger logger = Logger.getLogger( this.getClass().getName() );

    private FilterConfig config ;

    @Override
    public void init(FilterConfig config) throws ServletException {
        logger.info( "初始化" );
        this.config = config ;
    }

    @Override
    public void doFilter( ServletRequest req , ServletResponse resp , FilterChain chain )
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req ;
        String uri = request.getRequestURI();
        DispatcherType type = request.getDispatcherType();
        logger.info( "正在请求: " + uri + " , dispatch type : " + type );

        chain.doFilter( req , resp );

        logger.info( "被请求的资源: " + uri + " 已经返回" );

    }

    @Override
    public void destroy() {
        logger.info( "销毁" );
    }
}
